limsReport.editPage = {

    getGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-report-edit-list-grid',
            url: limsReport.baseUrl + '/queries/raw',
            service: 'limsReportServiceImpl',
            dbTable: 'T_LIMS_REPORT',
            columns: limsReport.getEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.getAsyncComp('lims-report-uploader').done(function (grid) {
                        grid.setOptions({
                            bizId: -1
                        })
                    });
                }
            },
            onRowActive: function (index, row) {
                Gikam.getAsyncComp('lims-report-uploader').done(function (grid) {
                    grid.setOptions({
                        bizId: row.id
                    })
                });
            },
            page: true,
            toolbar: [{
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                icon: 'submit', color: 'success',
                onClick: function () {
                    _this.submit();
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.PREVIEWBUILD',
                icon: 'export',
                onClick: function () {
                    var grid = Gikam.getComp('lims-report-edit-list-grid');
                    var selections = grid.getSelections();
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    workspace.window.showMask();
                    Gikam.getText(limsReport.baseUrl + '/create/reports' + Gikam.param({
                        ids: selections.map(function (item) {
                            return item.id;
                        }).join(',')
                    })).done(function (data) {
                        grid.refresh();
                    }).always(function () {
                        workspace.window.closeMask();
                    });
                }
            }, {
                type: 'button',
                text: 'GIKAM.FILE.BUTTON.UPLOAD_FILE',
                icon: 'upload',
                onClick: function () {
                    Gikam.create('simpleUploader', {
                        id: 'result-upload',
                        dbTable: 'T_LIMS_REPORT',
                        bizId: 'temp',
                        multiple: true,
                        autoClose: true,
                        bizCategory: 'upload',
                        //accept: ['doc', 'docx', 'pdf'],
                        onUploadSuccess: function (files) {
                            if (Gikam.isNotEmpty(files)) {
                                var copyFileUrl = Gikam.printf(limsReport.baseUrl + "/upload/tj/report");
                                workspace.window.showMask();
                                Gikam.postText(copyFileUrl, Gikam.getJsonWrapper(null, [null, files])).done(req => {
                                    req = JSON.parse(req)
                                    if (req.flag) {
                                        Gikam.getComp('lims-report-edit-list-grid').refresh()
                                    } else {
                                        Gikam.alert(req.msg)
                                    }
                                }).always(function () {
                                    workspace.window.closeMask();
                                });
                            }
                        }
                    })
                }
            }]
        }
    },

    getBaseInfoTab: function () {
        var _this = this;
        return {
            type: 'tab',
            panels: [{
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [{
                    type: 'uploader',
                    id: 'lims-report-uploader',
                    dbTable: 'T_LIMS_REPORT',
                }]
            }]
        }
    },

    submit: function () {
        var grid = Gikam.getComp('lims-report-edit-list-grid');
        Gikam.create('workflow').submit({
            data: grid.getSelections(),
            pageObject: limsReport,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            grid.refresh();
        });
    },

    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'lims-report-edit-list-layout',
            renderTo: workspace.window.$dom,
            north: {
                height: '60%',
                items: [this.getGridParam()]
            },
            center: {
                items: [this.getBaseInfoTab()]
            }
        });
    },

    init: function () {
        this.create();
    }
};
